package com.xb.cloud.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.common.model.PageParam;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;
import com.xb.cloud.user.model.EndUserFavoriteShop;
import com.xb.cloud.user.model.EndUserFavoriteShopExample;
import com.xb.cloud.user.model.EndUserFavoriteShopExamplePaged;
import com.xb.cloud.user.model.mapper.EndUserFavoriteShopMapper;
import com.xb.cloud.user.service.client.EndUserFavoriteShopService;
import com.xb.cloud.user.service.client.bo.EndUserFavoriteShopBo;

@Service
public class EndUserFavoriteShopServiceImpl extends AbstractService implements EndUserFavoriteShopService {

	@Resource
	EndUserFavoriteShopMapper endUserFavoriteShopMapper;

	@Override
	public int addFavoriteShop(EndUserFavoriteShopBo endUserFavoriteShopBo) {
		EndUserFavoriteShop endUserFavoriteShop = new EndUserFavoriteShop();
		endUserFavoriteShop.setCompanyId(endUserFavoriteShopBo.getCompanyId());
		endUserFavoriteShop.setEndUserId(endUserFavoriteShopBo.getEndUserId());
		endUserFavoriteShop.setShopId(endUserFavoriteShopBo.getShopId());
		endUserFavoriteShop.setId(ipKeyGenerator.generateKey());
		endUserFavoriteShop.setCreateTime(timeService.getDate());
		return endUserFavoriteShopMapper.insertSelective(endUserFavoriteShop);
	}

	@Override
	public EndUserFavoriteShopBo getFavoriteShop(EndUserFavoriteShopBo favoriteShopBo) {
		EndUserFavoriteShopExample example = new EndUserFavoriteShopExample();
		example.createCriteria().andCompanyIdEqualTo(favoriteShopBo.getCompanyId())
				.andEndUserIdEqualTo(favoriteShopBo.getEndUserId()).andShopIdEqualTo(favoriteShopBo.getShopId());
		List<EndUserFavoriteShop> selectByExample = endUserFavoriteShopMapper.selectByExample(example);
		return null;
	}

	@Override
	public Boolean isFollowed(EndUserFavoriteShopBo favoriteShopBo) {
		EndUserFavoriteShopExample example = new EndUserFavoriteShopExample();
		example.createCriteria().andCompanyIdEqualTo(favoriteShopBo.getCompanyId())
				.andEndUserIdEqualTo(favoriteShopBo.getEndUserId()).andShopIdEqualTo(favoriteShopBo.getShopId());
		List<EndUserFavoriteShop> favoriteShops = endUserFavoriteShopMapper.selectByExample(example);

		return favoriteShops.size() > 0;
	}

	@Override
	public Boolean cancelFollow(EndUserFavoriteShopBo favoriteShopBo) {
		EndUserFavoriteShopExample example = new EndUserFavoriteShopExample();
		example.createCriteria().andCompanyIdEqualTo(favoriteShopBo.getCompanyId())
				.andEndUserIdEqualTo(favoriteShopBo.getEndUserId()).andShopIdEqualTo(favoriteShopBo.getShopId());
		int deleted = endUserFavoriteShopMapper.deleteByExample(example);
		return deleted > 0;
	}

	@Override
	public ServiceReturnInfo getFavoriteShopList(EndUserFavoriteShopBo endUserFavoriteShopBo) {
		EndUserFavoriteShopExamplePaged example=new EndUserFavoriteShopExamplePaged();
		example.createCriteria()
			.andCompanyIdEqualTo(endUserFavoriteShopBo.getCompanyId())
			.andEndUserIdEqualTo(endUserFavoriteShopBo.getEndUserId());
		example.setPageParam(PageParam.resetPageParam(endUserFavoriteShopBo.getStartPage(),endUserFavoriteShopBo.getPageSize()));
		List<Long> favoriteShopIdList=endUserFavoriteShopMapper.selectByExamplePaged(example);
		if(favoriteShopIdList==null)
			return ServiceReturnUtils.error(ServiceReturnCodes.SYSTEM_EXCEPTION);
		return ServiceReturnUtils.success(favoriteShopIdList);
	}

}
